import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'dart:math';

class DeviceInfoService {
  static DeviceInfoPlugin? _deviceInfo;
  static String? _cachedDeviceId;
  static String? _cachedDeviceName;
  static String? _cachedDeviceModel;
  static String? _cachedSystemVersion;

  // 初始化设备信息服务
  static Future<void> init() async {
    _deviceInfo = DeviceInfoPlugin();
    await _loadDeviceInfo();
  }

  // 加载设备信息
  static Future<void> _loadDeviceInfo() async {
    try {
      _deviceInfo ??= DeviceInfoPlugin();

      // 检查是否为Web平台
      if (kIsWeb) {
        final webInfo = await _deviceInfo!.webBrowserInfo;
        _cachedDeviceId = 'web_${webInfo.browserName}_${DateTime.now().millisecondsSinceEpoch}';
        _cachedDeviceName = '${webInfo.browserName} Browser';
        _cachedDeviceModel = '${webInfo.browserName} ${webInfo.appVersion}';
        _cachedSystemVersion = webInfo.platform ?? 'Web Platform';

        debugPrint('Web设备信息:');
        debugPrint('  设备ID: $_cachedDeviceId');
        debugPrint('  设备名称: $_cachedDeviceName');
        debugPrint('  设备型号: $_cachedDeviceModel');
        debugPrint('  系统版本: $_cachedSystemVersion');

      } else if (Platform.isAndroid) {
        final androidInfo = await _deviceInfo!.androidInfo;
        _cachedDeviceId = androidInfo.id; // Android ID
        _cachedDeviceName = androidInfo.model;
        _cachedDeviceModel = '${androidInfo.brand} ${androidInfo.model}';
        _cachedSystemVersion = 'Android ${androidInfo.version.release}';
        
        debugPrint('Android设备信息:');
        debugPrint('  设备ID: $_cachedDeviceId');
        debugPrint('  设备名称: $_cachedDeviceName');
        debugPrint('  设备型号: $_cachedDeviceModel');
        debugPrint('  系统版本: $_cachedSystemVersion');
        
      } else if (Platform.isIOS) {
        final iosInfo = await _deviceInfo!.iosInfo;
        _cachedDeviceId = iosInfo.identifierForVendor; // iOS Vendor ID
        _cachedDeviceName = iosInfo.name;
        _cachedDeviceModel = iosInfo.model;
        _cachedSystemVersion = 'iOS ${iosInfo.systemVersion}';
        
        debugPrint('iOS设备信息:');
        debugPrint('  设备ID: $_cachedDeviceId');
        debugPrint('  设备名称: $_cachedDeviceName');
        debugPrint('  设备型号: $_cachedDeviceModel');
        debugPrint('  系统版本: $_cachedSystemVersion');
        
      } else if (Platform.isWindows) {
        final windowsInfo = await _deviceInfo!.windowsInfo;
        _cachedDeviceId = windowsInfo.deviceId;
        _cachedDeviceName = windowsInfo.computerName;
        _cachedDeviceModel = 'Windows PC';
        _cachedSystemVersion = windowsInfo.displayVersion;
        
        debugPrint('Windows设备信息:');
        debugPrint('  设备ID: $_cachedDeviceId');
        debugPrint('  设备名称: $_cachedDeviceName');
        debugPrint('  设备型号: $_cachedDeviceModel');
        debugPrint('  系统版本: $_cachedSystemVersion');
        
      } else if (Platform.isMacOS) {
        final macInfo = await _deviceInfo!.macOsInfo;
        _cachedDeviceId = macInfo.systemGUID;
        _cachedDeviceName = macInfo.computerName;
        _cachedDeviceModel = macInfo.model;
        _cachedSystemVersion = 'macOS ${macInfo.osRelease}';
        
        debugPrint('macOS设备信息:');
        debugPrint('  设备ID: $_cachedDeviceId');
        debugPrint('  设备名称: $_cachedDeviceName');
        debugPrint('  设备型号: $_cachedDeviceModel');
        debugPrint('  系统版本: $_cachedSystemVersion');
        
      } else if (Platform.isLinux) {
        final linuxInfo = await _deviceInfo!.linuxInfo;
        _cachedDeviceId = linuxInfo.machineId;
        _cachedDeviceName = linuxInfo.name;
        _cachedDeviceModel = 'Linux PC';
        _cachedSystemVersion = linuxInfo.prettyName;
        
        debugPrint('Linux设备信息:');
        debugPrint('  设备ID: $_cachedDeviceId');
        debugPrint('  设备名称: $_cachedDeviceName');
        debugPrint('  设备型号: $_cachedDeviceModel');
        debugPrint('  系统版本: $_cachedSystemVersion');
        
      } else {
        // 未知平台
        _cachedDeviceId = 'unknown';
        _cachedDeviceName = 'Unknown Device';
        _cachedDeviceModel = 'Unknown';
        _cachedSystemVersion = 'Unknown OS';
        
        debugPrint('未知平台设备信息');
      }
      
    } catch (e) {
      debugPrint('获取设备信息失败: $e');
      _cachedDeviceId = 'error';
      _cachedDeviceName = 'Error Device';
      _cachedDeviceModel = 'Error';
      _cachedSystemVersion = 'Error OS';
    }
  }

  // 获取设备ID
  static String getDeviceId() {
    return _cachedDeviceId ?? 'unknown';
  }

  // 获取设备名称
  static String getDeviceName() {
    return _cachedDeviceName ?? 'Unknown Device';
  }

  // 获取设备型号
  static String getDeviceModel() {
    return _cachedDeviceModel ?? 'Unknown';
  }

  // 获取系统版本
  static String getSystemVersion() {
    return _cachedSystemVersion ?? 'Unknown OS';
  }

  // 获取简短的设备ID（用于显示）
  static String getShortDeviceId() {
    final deviceId = getDeviceId();
    if (deviceId.length > 12) {
      return '${deviceId.substring(0, 6)}...${deviceId.substring(deviceId.length - 6)}';
    }
    return deviceId;
  }

  // 获取完整的设备信息
  static Map<String, String> getDeviceInfo() {
    return {
      'deviceId': getDeviceId(),
      'deviceName': getDeviceName(),
      'deviceModel': getDeviceModel(),
      'systemVersion': getSystemVersion(),
      'platform': Platform.operatingSystem,
    };
  }

  // 刷新设备信息
  static Future<void> refresh() async {
    await _loadDeviceInfo();
  }

  // 检查设备信息是否已加载
  static bool isLoaded() {
    return _cachedDeviceId != null;
  }
}
